package mi.payment.utils;

import java.util.Random;

import mi.payment.utils.iso8583.Utils;

public class RandomUtils {

    public static final String[] ALPHABET = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

    /**
     * 生成N字节随机数
     */
    public static byte[] getRandomBytes(int length) {
        byte[] kekBytes = new byte[length];
        Random mRandom = new Random();
        mRandom.nextBytes(kekBytes);
        return kekBytes;
    }

    /**
     * 生成N字节随机0-9 ASCII
     */
    public static byte[] getRandomBytesASCII(int length) {
        byte[] kekBytes = new byte[length];
        Random mRandom = new Random();
        for (int i = 0; i < length; i++) {
            kekBytes[i] = (byte) (mRandom.nextInt(10) + 48);
        }
        return kekBytes;
    }

    /**
     * 生产N字节的16进制数
     *
     * @return
     */
    public static byte[] getRandomBytesHex(int length) {
        String temp = "";
        int len = length * 2;//1字节2个16进制数
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            temp += ALPHABET[random.nextInt(16)];
        }
        return Utils.hexStr2Bytes(temp);
    }


}
